$(function(){
	
	$('#schoolLevelSum').change(function(){
		$('#schoolLevel').val($(this).val());
	}).focus(function(){
		$('#schoolLevel').val($(this).val());
	});
	$('#manMiddleLevelDetailSum').change(function(){
		$('#manMiddleLevel').val($(this).val());
	}).focus(function(){
		$('#manMiddleLevel').val($(this).val());
	});
	$('#deputyMiddleLevelDetailSum').change(function(){
		$('#deputyMiddleLevel').val($(this).val());
	}).focus(function(){
		$('#deputyMiddleLevel').val($(this).val());
	});
	$('#generalManagerDetailSum').change(function(){
		$('#generalManager').val($(this).val());
	}).focus(function(){
		$('#generalManager').val($(this).val());
	});
	$('#counselorDetailSum').change(function(){
		$('#counselor').val($(this).val());
	}).focus(function(){
		$('#counselor').val($(this).val());
	});
	
	/*** 初始化合计数 */
	initTotal();
	
	$('input[id$="Male"]').change(function(){
		var total1 = getTotoal('Male');
		$('#total1').html(total1);
	});
	
	$('input[id$="Female"]').change(function(){
		var total2 = getTotoal('Female');
		$('#total2').html(total2);
	});
	
	$('input[id$="ManHighLevel"]').change(function(){
		var total3 = getTotoal('ManHighLevel');
		$('#total3').html(total3);
	});
	
	$('input[id$="DeputyHighLevel"]').change(function(){
		var total4 = getTotoal('DeputyHighLevel');
		$('#total4').html(total4);
	});
	
	$('input[id$="MiddleLevel"]').change(function(){
		var total5 = getTotoal('MiddleLevel');
		$('#total5').html(total5);
	});
	
	$('input[id$="PrimaryLevel"]').change(function(){
		var total6 = getTotoal('PrimaryLevel');
		$('#total6').html(total6);
	});
	
	$('input[id$="OtherLevel"]').change(function(){
		var total7 = getTotoal('OtherLevel');
		$('#total7').html(total7);
	});
	
	$('input[id$="Doctor"]').change(function(){
		var total8 = getTotoal('Doctor');
		$('#total8').html(total8);
	});
	
	$('input[id$="Master"]').change(function(){
		var total9 = getTotoal('Master');
		$('#total9').html(total9);
	});
	
	$('input[id$="Bachelor"]').change(function(){
		var total10 = getTotoal('Bachelor');
		$('#total10').html(total10);
	});
	
	$('input[id$="Specialty"]').change(function(){
		var total11 = getTotoal('Specialty');
		$('#total11').html(total11);
	});
	
	/** 保存 */
	$('#btnSave').click(function(){
		/*$('#remark').validatebox({
			required : true,
			tipPosition : 'bottom',
			missingMessage : '任务内容不能为空'
		});
		$('#planAmount').numberbox({
			required : true,
			min:0,
			precision : 2,
			tipPosition : 'bottom',
			missingMessage : '计划预算不能为空',
			invalidMessage : '最小为0.00，只能输入两位小数'
		})
		$('#startTime').validatebox({
			required : true,
			tipPosition : 'bottom',
			missingMessage : '任务起始时间不能为空'
		});
		$('#endTime').validatebox({
			required : true,
			tipPosition : 'bottom',
			missingMessage : '任务结束时间不能为空'
		});
		var valid = $('#form').form('validate');*/
		/*if(valid) {
			$('#form').ajaxSubmit({
				url : 'task/save',
				success : function(data) {
					if (!data.success) {
						$.messager.alert('提醒', data.message,'info');
					} else {
						$.messager.alert('提醒', data.message,'info');
						$('#form').form('clear');
						parent.reSearch();
						parent.closeEasyUiWindow('win');
					}
				}
			});
		}*/
		$("body").mask();
		$('#form').ajaxSubmit({
			url : 'staffCountInfo/save.shtml',
			success : function(data) {
				$("body").unmask();
				if (!data.success) {
					$.messager.alert('提醒', data.message,'info');
				} else {
					$.messager.alert('提醒', data.message,'info', function() {
						$('#form').form('clear');
						parent.reSearch();
						parent.closeEasyUiWindow('win');
					});
				}
			}
		});
	});
	
	/** 取消 */
	$('#btnCancel').click(function(){
		$('#form').form('clear');
		parent.closeEasyUiWindow('win');
	});
});

/**
 * 初始化合计
 */
function initTotal() {
	var total1 = getTotoal('Male');
	$('#total1').html(total1);
	var total2 = getTotoal('Female');
	$('#total2').html(total2);
	var total3 = getTotoal('ManHighLevel');
	$('#total3').html(total3);
	var total4 = getTotoal('DeputyHighLevel');
	$('#total4').html(total4);
	var total5 = getTotoal('MiddleLevel');
	$('#total5').html(total5);
	var total6 = getTotoal('PrimaryLevel');
	$('#total6').html(total6);
	var total7 = getTotoal('OtherLevel');
	$('#total7').html(total7);
	var total8 = getTotoal('Doctor');
	$('#total8').html(total8);
	var total9 = getTotoal('Master');
	$('#total9').html(total9);
	var total10 = getTotoal('Bachelor');
	$('#total10').html(total10);
	var total11 = getTotoal('Specialty');
	$('#total11').html(total11);
}

/**
 * 合计
 * @param key
 * @returns {Number}
 */
function getTotoal(key) {
	var total = 0;
	$('input[id$="' + key + '"]').each(function(i, val){
		var t = $(this).val();
		total += Number(t);
	});
	return total;
}